package com.leetcode.LC;

public class LC733 {
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        if (image[sr][sc]==newColor){
            return image;
        }
        int oldColor=image[sr][sc];
        dfs(image, sr, sc, newColor,oldColor);
        return image;
    }
    public void dfs(int[][] grid, int row, int col, int newColor,int oldColor) {
        if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] != oldColor) {
            return;
        }
        grid[row][col] = newColor;
        dfs(grid, row, col - 1, newColor,oldColor);
        dfs(grid, row, col + 1, newColor,oldColor);
        dfs(grid, row - 1, col, newColor,oldColor);
        dfs(grid, row + 1, col, newColor,oldColor);
    }
}
